package com.yangxinquan.test;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class MyJFrame extends JFrame implements ActionListener {
    JButton jtb1 = new JButton("点我啊");
    JButton jtb2 = new JButton("别点我");

    //直接构造方法调用
    public MyJFrame() {
        Random r = new Random();
        JFrame jF = new JFrame();
        jF.setSize(603, 680);
        jF.setTitle("事件展示");
        //设置关闭模式
        jF.setDefaultCloseOperation(3);
        //关内容默认居中
        jF.setLayout(null);
        //设置置顶
        jF.setAlwaysOnTop(true);
        //设置框居中
        jF.setLocationRelativeTo(null);


        jtb1.setBounds(0, 0, 100, 50);
        jtb2.setBounds(100, 0, 100, 50);

        jtb1.addActionListener(this);
        jtb2.addActionListener(this);

        jF.getContentPane().add(jtb1);
        jF.getContentPane().add(jtb2);

        jtb1.setVisible(true);
        jtb2.setVisible(true);

        jF.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        Random r = new Random();
        if (source == jtb1) {
            jtb1.setBounds(r.nextInt(500), r.nextInt(500), 100, 50);
        } else if (source == jtb2) {
            jtb2.setBounds(0, 0, 100, 100);
        }
    }
}
